<template>
  <div class="publish">
    <van-nav-bar title="发布房源" left-arrow @click-left="$router.back()" />
    <van-cell class="title" title="房源信息" />
    <van-form @submit="onSubmit">
      <van-cell
        title="小区名字"
        value="请输入小区内容"
        is-link
        to="/rent/search"
      />
      <van-field label="租   金" placeholder="请输入租金/月">
        <template #extra> ￥/月 </template>
      </van-field>
      <van-field label="建筑面积" placeholder="请输入建筑面积">
        <template #extra> m² </template>
      </van-field>
      <house-type @getRoomType="getRoomType" :row="roomType" />
      <house-floor @getFloor="getFloor" :row="floor" />
      <house-direction @getOriented="getOriented" :row="oriented" />
      <van-cell title="房源标题" />
      <van-field placeholder="请输入标题（例如：整租 小区名 2室 5000单元）" />
      <van-cell title="房源图像" />
      <!-- <van-uploader :after-read="afterRead" max-count="3" :before-read="beforeRead" /> -->
      <van-uploader v-model="fileList" multiple :max-count="3" />
      <van-cell title="房源配置" />
      <van-grid :border="false" :column-num="5">
        <van-grid-item
          v-for="item in cesi"
          :key="item.label"
          :text="item.label"
        >
          <template #icon>
            <i :class="item.ic" class="iconfont"></i>
          </template>
        </van-grid-item>
      </van-grid>
      <van-cell class="desc" title="房源描述" />
      <van-field
        class="desc-mess"
        rows="1"
        autosize
        type="textarea"
        placeholder="请输入房屋描述信息"
      />
      <div class="btn">
        <van-button plain type="primary" @click="cancel">取消</van-button>
        <van-button type="primary">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { Dialog } from 'vant'
import { getHouseNeed } from '@/api/house.js'
import HouseType from './components/HouseType.vue'
import HouseFloor from './components/HouseFloor.vue'
import HouseDirection from './components/HouseDirection.vue'

export default {
  name: 'publish',
  components: { HouseType, HouseFloor, HouseDirection },
  data () {
    return {
      fileList: [],
      supportings: [],
      floor: [],
      oriented: [],
      roomType: [],
      cesi: [],
      form: {
        title: '',
        description: '',
        houseImg: '',
        oriented: '',
        supporting: '',
        price: '',
        roomType: '',
        size: '',
        floor: '',
        community: ''
      },
      icons: [
        { ic: 'icon-dianshiji' },
        { ic: 'icon-bingxiang' },
        { ic: 'icon-xiyiji' },
        { ic: 'icon-kongtiao' },
        { ic: 'icon-reshuiqi' },
        { ic: 'icon-iconyihuifu' },
        { ic: 'icon-nuanqi' },
        { ic: 'icon-wifi' },
        { ic: 'icon-yigui' },
        { ic: 'icon-tianranqi' }
      ]
    }
  },

  created () {
    this.loadHouseNeed()
    this.load()
  },

  methods: {
    onSubmit () {
      // console.log('提交了')
    },
    async loadHouseNeed () {
      const res = await getHouseNeed(this.$store.state.user.token)
      console.log(res)
      this.supportings = res.data.body.supporting
      for (let i = 0; i < this.icons.length; i++) {
        this.cesi.push({ ...this.supportings[i], ...this.icons[i] })
      }
      this.floor = res.data.body.floor
      this.oriented = res.data.body.oriented
      this.roomType = res.data.body.roomType
    },
    cancel () {
      Dialog.confirm({
        title: '提示',
        message: '是否放弃编辑'
      })
        .then(() => {
          // console.log('确认了')
          this.$router.push('/rent')
        })
    },
    getRoomType (e) {
      this.form.roomType = e.value
    },
    getOriented (e) {
      this.form.oriented = e.value
    },
    getFloor (e) {
      this.form.floor = e.value
    },
    load () {
      console.log(this.supportings)
      // this.supportings.push(...this.icons)
      console.log(this.supportings)
    }
  }
}
</script>

<style scoped lang="less">
.publish {
  .van-nav-bar {
    background-color: #21b97a;
    /deep/ .van-nav-bar__title {
      color: #fff;
    }
    /deep/ .van-icon {
      color: #fff;
    }
  }
  .title {
    color: #21b97a;
  }
  /deep/.van-uploader__wrapper {
    margin-left: 20px;
  }
  .preview-cover {
    position: absolute;
    bottom: 0;
    box-sizing: border-box;
    width: 100%;
    padding: 4px;
    color: #fff;
    font-size: 12px;
    text-align: center;
    background: rgba(0, 0, 0, 0.3);
  }
  .iconfont {
    font-size: 40px;
  }
  .desc {
    margin-top: 20px;
  }
  .desc-mess {
    margin-bottom: 120px;
    /deep/.van-field__body {
      min-height: 120px;
      /deep/.van-field__control {
        min-height: 120px;
      }
    }
  }

  // /deep/.van-grid-item__content {
  //   padding: 0;
  // }
  .btn {
    position: fixed;
    bottom: 0;
    width: 100%;

    .van-button {
      width: 50%;
    }
  }
}
</style>
